/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Core;

import Common.Log;
import Common.MessageBox;
import java.io.*;
import java.util.*;
import org.w3c.dom.Document;
import Common.XMLParser;
import Database.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author harish
 */
public class Configuration
{
    private static int _currentConfiguration=1;
    private static List<Appliance> _essentialAppliances=null;
    private static List<Appliance> _nonEssentialAppliances=null;
    private static String _url="";
    private static String _user="";
    private static String _pwd="";
    private static int _timeScale=1;
    private static String _logFilePath="";
    private static int _homes=10;
    private static String _outputType="";
    private static String _outputJRXML="";
    private static String _outputPath="";
    private static String _outputDefault="";

    private static String _xmlFileName="src\\Core\\ConfigXML.xml";
    private Document doc=null;

    private static Configuration _instance=null;

    private Configuration(){}

    public static Configuration GetInstance()
    {
        if(_instance==null)
        {
            _instance=new Configuration();
        }
        return _instance;
    }
    
    public void Configure()
    {
        try
        {
            Log.GetInstance().Create();
            ReadConfigXML();  
            Database.GetInstance().Configure();
            _currentConfiguration=ReadCurrentConfiguration();
            if(!CompareConfig())
            {
                InsertConfig();
                _currentConfiguration=ReadCurrentConfiguration();
            }                                                 
            ReadEssentialAppliances();
            ReadNonEssentialAppliances();
            Log.GetInstance().Write("Successfully configured");
        }
        catch(Exception e)
        {
            MessageBox.GetInstance().Show("Error in Configuring"+e.getMessage());
        }
    }
    
    private void ReadConfigXML()
    {
        try
        {
            doc=XMLParser.PrepareDoc(_xmlFileName);
           _logFilePath=XMLParser.Parse("LogFile","path",doc);
           _url=XMLParser.Parse("Database","URL",doc);
           _user=XMLParser.Parse("Database","user",doc);
           _pwd=XMLParser.Parse("Database","pwd",doc);
           _homes=Integer.parseInt(XMLParser.Parse("Simulator","homes",doc));
           _timeScale=Integer.parseInt(XMLParser.Parse("Simulator","TimeScale",doc));
           _outputPath=XMLParser.Parse("Output","report",doc);
           _outputJRXML=XMLParser.Parse("Output","jrxml",doc);
           _outputDefault=XMLParser.Parse("Output","default",doc);
           _outputType=XMLParser.Parse("Output","type",doc);
           Log.GetInstance().Write("Initial Configurations from xml file read successfully");
        }
        catch(Exception e)
        {
            try
            {
            Log.GetInstance().Write("Error in reading xml file of configuration" + e.getMessage());
            }
            catch(IOException i)
            {

            }
        }
    }
    
    private void ReadEssentialAppliances()
    {
        try
        {
            _essentialAppliances=Database.GetInstance().ReadEssentialAppliances();
            Log.GetInstance().Write("Essential Appliances are read successfully");
        }
        catch (Exception ex) 
        {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex); 
            try
            {
            Log.GetInstance().Write("Error in reading essential appliances");
            }
            catch(IOException i)
            {

            }
                
        }
        finally 
        {
            
        }
        
    }
    
    private void ReadNonEssentialAppliances()
    {
        
        try
        {
            _nonEssentialAppliances=Database.GetInstance().ReadNonEssentialAppliances();
            Log.GetInstance().Write("Non-Essential Appliances are read successfully");
        }
        catch (Exception ex) 
        {
            Logger lgr = Logger.getLogger(Database.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex); 
            try
            {
            Log.GetInstance().Write("Error in reading non-essential appliances");
            }
            catch(IOException i)
            {

            }
                
        }
        finally 
        {
            
        }
        
    }
    
    private String ReadConfigFromDB()
    {
        try
        {
            return Database.GetInstance().ReadCurrentConfigAsString(_currentConfiguration);
        }
        catch(Exception e)
        {
            return "";
        }
    }
    
    private String ReadConfigXMLAsString()
    {     
        try
        {
        FileInputStream input = new FileInputStream(_xmlFileName);
        byte[] fileData = new byte[input.available()];

        input.read(fileData);
        input.close();

        return new String(fileData, "UTF-8");
        }
        catch(Exception e)
        {
            return "";
        }
    }
    
    private boolean CompareConfig()
    {
        try
        {
            String XML=ReadConfigXMLAsString();
            String DB=ReadConfigFromDB();
            return XML.equals(DB);
            
        }
        catch(Exception e)
        {
            try
            {
            Log.GetInstance().Write("Error in comparing Configuration");
            }
            catch(IOException i)
            {

            }
            return false;
        }
    }
    
    private void InsertConfig()
    {
        Database.GetInstance().InsertConfiguration(ReadConfigXMLAsString());
    }
    
    private int ReadCurrentConfiguration()
    {
        return Database.GetInstance().ReadCurrentConfiguration();
    }

   public int GetCurrentConfiguration()
   {
       return _currentConfiguration;
   }

    public String GetURL()
    {
        return _url;
    }

    public String GetUser()
    {
        return _user;
    }

    public String GetPwd()
    {
        return _pwd;
    }

    public int GetHomes()
    {
        return _homes;
    }

    public int GetTimeScale()
    {
        return _timeScale;
    }

    public String GetLogFilePath()
    {
        return _logFilePath;
    }
    
    public List<Appliance> GetEssentialAppliances()
    {
        return _essentialAppliances;
    }
    
    public List<Appliance> GetNonEssentialAppliances()
    {
        return _nonEssentialAppliances;
    }
    
    public int GetNoOfNonEssentialAppliances()
    {
        return (_nonEssentialAppliances.size());
    }
    
    public String GetOutputPath()
    {
        return _outputPath;
    }
    
    public String GetOutputDefault()
    {
        return _outputDefault;
    }
    
    public String GetOutputJRXML()
    {
        return _outputJRXML;
    }
    
}
